home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GENCSRC.ZIP / BIGDYNL.C < prev    next >
C/C++ Source or Header  |  1987-11-21  |  1KB  |  37 lines

  1.                                       /* Chapter 12 - Program 2 */
  2. main()
  3. {
  4. struct animal {
  5.    char name[25];
  6.    char breed[25];
  7.    int age;
  8. } *pet[12], *point;   /* this defines 13 pointers, no variables */
  9. int index;
  10.  
  11.             /* first, fill the dynamic structures with nonsense */
  12.    for (index = 0;index < 12;index++) {
  13.       pet[index] = (struct animal *)malloc(sizeof(struct animal));
  14.       strcpy(pet[index]->name,"General");
  15.       strcpy(pet[index]->breed,"Mixed Breed");
  16.       pet[index]->age = 4;
  17.    }
  18.  
  19.    pet[4]->age = 12;        /* these lines are simply to        */
  20.    pet[5]->age = 15;        /*      put some nonsense data into */
  21.    pet[6]->age = 10;        /*            a few of the fields.  */
  22.  
  23.        /* now print out the data described above */
  24.  
  25.    for (index = 0;index <12;index++) {
  26.       point = pet[index];
  27.       printf("%s is a %s, and is %d years old.\n", point->name,
  28.               point->breed, point->age);
  29.    }
  30.  
  31.        /* good programming practice dictates that we free up the */
  32.        /* dynamically allocated space before we quit.            */
  33.  
  34.    for (index = 0;index < 12;index++)
  35.       free(pet[index]);
  36. }
  37.